package com.vichat.common.util;

import org.springframework.web.multipart.MultipartFile;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Created by ranjx on 2017/4/26.
 */
public class FileUtil {

    /**
     * 创建存储文件夹
     * @param id
     * @param path
     * @return
     */
    public static String mkdirFile(String id,String path) {
        String addZeroForUid = addZeroForNum(id,9);
        String str1 = addZeroForUid.substring(0,3);
        String str2 = addZeroForUid.substring(3,5);
        String str3 = addZeroForUid.substring(5,7);
        String str4 = addZeroForUid.substring(7,9);
        String filePath = path + "/" + str1;
        File str1Path = new File(filePath);
        if(!str1Path.exists()){
            str1Path.mkdir();
        }
        filePath += "/" + str2;
        File str2Path = new File(filePath);
        if(!str2Path.exists()){
            str2Path.mkdir();
        }
        filePath += "/" + str3;
        File str3Path = new File(filePath);
        if(!str3Path.exists()){
            str3Path.mkdir();
        }
        filePath += "/" + str4;
        File str4Path = new File(filePath);
        if(!str4Path.exists()){
            str4Path.mkdir();
        }
        return filePath;
    }

    /**
     * 获取访问地址
     * @param id
     * @param path
     * @param fileSuffix
     * @return
     */
    public static String getAttachUrl(long id, String path, String fileSuffix) {
        String addZeroForUid = addZeroForNum(String.valueOf(id),9);
        String str1 = addZeroForUid.substring(0,3);
        String str2 = addZeroForUid.substring(3,5);
        String str3 = addZeroForUid.substring(5,7);
        String str4 = addZeroForUid.substring(7,9);
        return new StringBuilder().append(path).append("/").append(str1).append("/").append(str2).append("/").append(str3).append("/").append(str4).append("/").append(id).append(fileSuffix).toString();
    }

    /**
     * 位数不够左边补0
     * @param str
     * @param length
     * @return
     */
    public static String addZeroForNum(String str, int length) {
        int strLen = str.length();
        if(strLen < length) {
            while (strLen < length) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);//左边补0
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }

    /**
     * 文件存储本地
     * @param file
     * @param id
     * @param fileSuffix
     */
    public static String fileToLocal(MultipartFile file, long id, String fileSuffix) {
        String fileName = "";
        if (!file.isEmpty()) {
            try {
                //String newFileName = addZeroForNum(String.valueOf(id),9).substring(7,9) + fileSuffix;
                String newFileName = id + fileSuffix;
                String filePath = mkdirFile(String.valueOf(id),Configure.getAvaFileDir());
                File fileDir = new File(filePath);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                fileName = filePath + System.getProperty("file.separator") + newFileName;
                file.transferTo(new File(fileName));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return fileName;
    }

    public static void main(String[]args) {
        String url = FileUtil.getAttachUrl(300000000l,"http://www.g-talking.com","_avatar.jpg");
        System.out.print(url);
    }

    /**
     * 获取图片宽度
     * @param file 图片文件
     * @return 宽度
     */
    public static int getImgWidth(File file) {
        InputStream is = null;
        BufferedImage src = null;
        int ret = -1;
        try {
            is = new FileInputStream(file);
            src = javax.imageio.ImageIO.read(is);
            ret = src.getWidth(null); // 得到源图宽
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 获取图片高度
     * @param file 图片文件
     * @return 高度
     */
    public static int getImgHeight(File file) {
        InputStream is = null;
        BufferedImage src = null;
        int ret = -1;
        try {
            is = new FileInputStream(file);
            src = javax.imageio.ImageIO.read(is);
            ret = src.getHeight(null); // 得到源图高
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }
}
